import axios from 'axios'
import qs from 'qs'

//提取公共的基路径
const instance = axios.create({
  // baseURL: '/api', //请求的基路径
  baseURL: 'http://127.0.0.1:5000', //请求的基路径
  timeout: 5000, //请求超时时间
})

//拦截器拦截请求和响应
instance.interceptors.request.use(
  (config) => {
    const token = JSON.parse(localStorage.getItem('t_k') as string)
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  (err) => {
    console.log(err)
  },
)

instance.interceptors.response.use(
  (response) => {
    return response
  },
  (err) => {
    console.log(err)
  },
)
//定义get请求params接口
interface paramsType {
  currentPage:number,
  pageSize:number
}
//定义post请求data接口
interface dataType {
  account:string,
  password:string
}

const get = async (url: string, params: paramsType) => {
  return instance.get(url, { params })
}
const getCategories = async (url: string) => {
  return instance.get(url)
}

const post = async (url: string, data: dataType) => {
  return await instance.post(url, qs.stringify(data))
}
const shopPost = async (url: string, data: dataType) => {
  return await instance.post(url, data)
}
const put = () => async (url: string, data: dataType) => {
    return await instance.put(url, qs.stringify(data))
  }
const del = () => {}

export default {
  get,
  post,
  put,
  del,
  getCategories,
  shopPost
}
